{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 10.9 \u5c06\u6587\u4ef6\u5939\u52a0\u5165\u5230sys.path\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u65e0\u6cd5\u5bfc\u5165\u4f60\u7684Python\u4ee3\u7801\u56e0\u4e3a\u5b83\u6240\u5728\u7684\u76ee\u5f55\u4e0d\u5728sys.path\u91cc\u3002\u4f60\u60f3\u5c06\u6dfb\u52a0\u65b0\u76ee\u5f55\u5230Python\u8def\u5f84\uff0c\u4f46\u662f\u4e0d\u60f3\u786c\u94fe\u63a5\u5230\u4f60\u7684\u4ee3\u7801\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6709\u4e24\u79cd\u5e38\u7528\u7684\u65b9\u5f0f\u5c06\u65b0\u76ee\u5f55\u6dfb\u52a0\u5230sys.path\u3002\u7b2c\u4e00\u79cd\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528PYTHONPATH\u73af\u5883\u53d8\u91cf\u6765\u6dfb\u52a0\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sys\nsys.path"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u81ea\u5b9a\u4e49\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u8fd9\u6837\u7684\u73af\u5883\u53d8\u91cf\u53ef\u5728\u7a0b\u5e8f\u542f\u52a8\u65f6\u8bbe\u7f6e\u6216\u901a\u8fc7shell\u811a\u672c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7b2c\u4e8c\u79cd\u65b9\u6cd5\u662f\u521b\u5efa\u4e00\u4e2a.pth\u6587\u4ef6\uff0c\u5c06\u76ee\u5f55\u5217\u4e3e\u51fa\u6765\uff0c\u50cf\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# myapplication.pth\n/some/dir\n/other/dir"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a.pth\u6587\u4ef6\u9700\u8981\u653e\u5728\u67d0\u4e2aPython\u7684site-packages\u76ee\u5f55\uff0c\u901a\u5e38\u4f4d\u4e8e/usr/local/lib/python3.3/site-packages \u6216\u8005 ~/.local/lib/python3.3/sitepackages\u3002\u5f53\u89e3\u91ca\u5668\u542f\u52a8\u65f6\uff0c.pth\u6587\u4ef6\u91cc\u5217\u4e3e\u51fa\u6765\u7684\u5b58\u5728\u4e8e\u6587\u4ef6\u7cfb\u7edf\u7684\u76ee\u5f55\u5c06\u88ab\u6dfb\u52a0\u5230sys.path\u3002\u5b89\u88c5\u4e00\u4e2a.pth\u6587\u4ef6\u53ef\u80fd\u9700\u8981\u7ba1\u7406\u5458\u6743\u9650\uff0c\u5982\u679c\u5b83\u88ab\u6dfb\u52a0\u5230\u7cfb\u7edf\u7ea7\u7684Python\u89e3\u91ca\u5668\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6bd4\u8d77\u8d39\u529b\u5730\u627e\u6587\u4ef6\uff0c\u4f60\u53ef\u80fd\u4f1a\u503e\u5411\u4e8e\u5199\u4e00\u4e2a\u4ee3\u7801\u624b\u52a8\u8c03\u8282sys.path\u7684\u503c\u3002\u4f8b\u5982:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sys\nsys.path.insert(0, '/some/dir')\nsys.path.insert(0, '/other/dir')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u867d\u7136\u8fd9\u80fd\u201c\u5de5\u4f5c\u201d\uff0c\u4f46\u662f\u5728\u5b9e\u8df5\u4e2d\u6781\u4e3a\u8106\u5f31\uff0c\u5e94\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u95ee\u9898\u662f\uff0c\u5b83\u5c06\u76ee\u5f55\u540d\u786c\u7f16\u7801\u5230\u4e86\u4f60\u7684\u6e90\u4ee3\u7801\u3002\u5982\u679c\u4f60\u7684\u4ee3\u7801\u88ab\u79fb\u5230\u4e00\u4e2a\u65b0\u7684\u4f4d\u7f6e\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u7ef4\u62a4\u95ee\u9898\u3002\u66f4\u597d\u7684\u505a\u6cd5\u662f\u5728\u4e0d\u4fee\u6539\u6e90\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u5c06path\u914d\u7f6e\u5230\u5176\u4ed6\u5730\u65b9\u3002\u5982\u679c\u60a8\u4f7f\u7528\u6a21\u5757\u7ea7\u7684\u53d8\u91cf\u6765\u7cbe\u5fc3\u6784\u9020\u4e00\u4e2a\u9002\u5f53\u7684\u7edd\u5bf9\u8def\u5f84\uff0c\u6709\u65f6\u4f60\u53ef\u4ee5\u89e3\u51b3\u786c\u7f16\u7801\u76ee\u5f55\u7684\u95ee\u9898\uff0c\u6bd4\u5982__file__\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sys\nfrom os.path import abspath, join, dirname\nsys.path.insert(0, join(abspath(dirname(__file__)), 'src'))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u5c06src\u76ee\u5f55\u6dfb\u52a0\u5230path\u91cc\uff0c\u548c\u6267\u884c\u63d2\u5165\u6b65\u9aa4\u7684\u4ee3\u7801\u5728\u540c\u4e00\u4e2a\u76ee\u5f55\u91cc\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "site-packages\u76ee\u5f55\u662f\u7b2c\u4e09\u65b9\u5305\u548c\u6a21\u5757\u5b89\u88c5\u7684\u76ee\u5f55\u3002\u5982\u679c\u4f60\u624b\u52a8\u5b89\u88c5\u4f60\u7684\u4ee3\u7801\uff0c\u5b83\u5c06\u88ab\u5b89\u88c5\u5230site-packages\u76ee\u5f55\u3002\u867d\u7136\u7528\u4e8e\u914d\u7f6epath\u7684.pth\u6587\u4ef6\u5fc5\u987b\u653e\u7f6e\u5728site-packages\u91cc\uff0c\u4f46\u5b83\u914d\u7f6e\u7684\u8def\u5f84\u53ef\u4ee5\u662f\u7cfb\u7edf\u4e0a\u4efb\u4f55\u4f60\u5e0c\u671b\u7684\u76ee\u5f55\u3002\u56e0\u6b64\uff0c\u4f60\u53ef\u4ee5\u628a\u4f60\u7684\u4ee3\u7801\u653e\u5728\u4e00\u7cfb\u5217\u4e0d\u540c\u7684\u76ee\u5f55\uff0c\u53ea\u8981\u90a3\u4e9b\u76ee\u5f55\u5305\u542b\u5728.pth\u6587\u4ef6\u91cc\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}